//
//  HESheetListView.m
//  HealthElderly
//
//  Created by 吴欧 on 2021/5/12.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HESheetListView.h"

/// 标题栏高度（包括确定/取消，和年/月/日）
#define KAlterHeight KScreenHeight * 0.5

/// 主题颜色
#define kThemeColor [UIColor blueColor]

@interface HESheetListView ()<UITableViewDelegate, UITableViewDataSource>

// 中间标题
@property (nonatomic, strong) UILabel *titleLabel;

// 蒙层视图
@property (nonatomic, strong) UIView *maskView;

// 弹出背景视图
@property (nonatomic, strong) UIView *alertView;

@property (nonatomic, strong) GDTableView *tableView;



@end

@implementation HESheetListView



- (void)show {
     
    [self initUI];
     
    UIWindow *keyWindow = BRGetKeyWindow();
    [keyWindow addSubview:self];
    // 动画前初始位置
    CGRect rect = self.alertView.frame;
    rect.origin.y = self.bounds.size.height;
    self.alertView.frame = rect;
    // 弹出动画
    self.maskView.alpha = 1;
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y -= BR_BOTTOM_MARGIN + KAlterHeight;
        self.alertView.frame = rect;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)initUI {
    self.frame = [UIScreen mainScreen].bounds;
    // 设置子视图的宽度随着父视图变化
    self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    [self addSubview:self.maskView];
    
    [self addSubview:self.alertView];
    
    [self.alertView addSubview:self.titleLabel];

    [self.alertView addSubview:self.tableView];

}

#pragma mark -
#pragma mark - 蒙层视图
- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2f];
        // 设置子视图的大小随着父视图变化
        _maskView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _maskView.userInteractionEnabled = YES;
        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapMaskView:)];
        [_maskView addGestureRecognizer:myTap];
    }
    return _maskView;
}

#pragma mark - 弹框视图
- (UIView *)alertView {
    if (!_alertView) {
        _alertView = [[UIView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height - KAlterHeight - BR_BOTTOM_MARGIN, self.bounds.size.width,  BR_BOTTOM_MARGIN + KAlterHeight)];
        _alertView.backgroundColor = [UIColor whiteColor];
        _alertView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
//        [_alertView hg_setCornerOnTopWithRadius:10];
    }
    return _alertView;
}

#pragma mark - 中间标题label
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, self.bounds.size.width, 44)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = DYBlodFont(19);
        _titleLabel.textColor = CNavBgColor;
        _titleLabel.text = _title;
        
        _titleLabel.qmui_borderColor = CSplitLineColor;
        _titleLabel.qmui_borderWidth = 1;
        _titleLabel.qmui_borderPosition = QMUIViewBorderPositionBottom;
    }
    return _titleLabel;
}

#pragma mark - 列表
- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame: CGRectZero style:UITableViewStylePlain];
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.separatorColor = KHexColor(@"EEEEEE");
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        [self.alertView addSubview: _tableView];
        
        [_tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"UITableViewCell"];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLabel.mas_bottom);
            make.right.left.equalTo(self.alertView);
            make.bottom.equalTo(self.alertView).offset(-ZXSafeAreaBottom);
        }];
    }
    return _tableView;
}

#pragma mark -

- (void)finishClick {
    [self dismiss];
}

#pragma mark - 点击蒙层视图事件
- (void)didTapMaskView:(UITapGestureRecognizer *)sender {
    [self dismiss];
}

#pragma mark - 关闭选择器视图
- (void)dismiss {
    // 关闭动画
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y +=  BR_BOTTOM_MARGIN + KAlterHeight;
        self.alertView.frame = rect;
        self.maskView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
        
        cell.textLabel.font = DYFont(17);
        cell.textLabel.numberOfLines = 0;
        [cell.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(cell.contentView).offset(20);
                make.left.equalTo(cell.contentView).offset(15);
                make.right.equalTo(cell.contentView).offset(-15);
                make.bottom.equalTo(cell.contentView).offset(-20);
            }];
        }];
    }
    HEApplyModel *model = _dataSource[indexPath.row];
    cell.textLabel.text = model.hospital_name;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HEApplyModel *model = _dataSource[indexPath.row];
    return [tableView fd_heightForCellWithIdentifier: @"UITableViewCell" cacheByIndexPath:indexPath configuration:^(UITableViewCell *cell) {
        cell.textLabel.text = model.hospital_name;
    }];
}
 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HEApplyModel *model = _dataSource[indexPath.row];
    if ([_delegate respondsToSelector:@selector(sheetListViewSelectItemWith:)]) {
        [_delegate sheetListViewSelectItemWith:model];
    }
    
    [self dismiss];
}

@end
